package com.dangdang.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.smartstruts.action.Action;
import org.smartstruts.action.ActionForm;

import com.dangdang.domain.User;
import com.dangdang.form.LoginForm;
import com.dangdang.service.ServiceFactory;
import com.dangdang.service.UserService;
import com.dangdang.util.Constants;
import com.dangdang.util.ValidateUtils;

/**
 * 用户登录/登出综合控制类
 * 
 * @author Wangpeihu
 * @team [Never give up]
 * @since JDK1.6(建议)
 * @date 2010-1-18
 */
public class LoginAction extends Action {

	UserService us = ServiceFactory.getUserService();

	/**
	 * 验证成功跳转至主页面<br>
	 * 验证失败返回至登录界面<br>
	 */

	@Override
	public String execute(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Thread.sleep(20);
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 用户登录界面请求
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String userLoginForm(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		return Constants.FORWARD_SUCCESS;
	}

	/**
	 * 用户登录验证
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String loginVerify(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		LoginForm lf = (LoginForm) form;
		/** 登录检测该用户是否存在 */
		User user = us.userVerify(lf.getNickName(), lf.getPassword());
		if (null != user) {
			/** 将用户状态设为登录状态 */
			ValidateUtils.setLoginState(request, user);
			return Constants.FORWARD_SUCCESS;
		} else {
			return Constants.FORWARD_FAIL;
		}
	}

	/**
	 * 用户登出请求
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String logoutVerify(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		if (ValidateUtils.validate(request)) {
			/** 将用户状态设为登出状态 */
			ValidateUtils.setLogoutState(request);
			return Constants.FORWARD_SUCCESS;
		} else {
			return Constants.FORWARD_FAIL;
		}
	}

	/**
	 * 超级管理员登录验证
	 * 
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public String manageLoginCheck(ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		return Constants.FORWARD_SUCCESS;
	}
}
